<?php

namespace App\Console\Commands;

use App\Models\BasicInfoAddress;
use App\Models\BasicInfoGraduatedSchool;
use App\Models\BasicInfoLearnHistory;
use App\Models\BasicInfoPosition;
use App\Models\BasicInfoWorkHistory;
use App\Models\BasicTitleInfo;
use Illuminate\Console\Command;

class UpdateBasicInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ClearRedundantData:basicInfo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '清除基本信息数据表中冗余数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("---清除,开始:".date('Y-m-d H:i:s')."---");
        $this->taskRun();
        // Log::channel('daily')->info('数据');
        $this->info("---清除,结束:".date('Y-m-d H:i:s')."---");
    }

    public function taskRun()
    {
        $this->clearAddressTable();
        $this->clearGraduatedSchool();
        $this->clearLearnHistory();
        $this->clearPosition();
        $this->clearWorkHistory();
        $this->clearTitleInfo();
    }

    public function clearAddressTable()
    {
        $address = BasicInfoAddress::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'))->first();
            BasicInfoAddress::where('user_id', $key)->where('id', '!=', $maxItem->id)->update(['deleted' => 1]);
        });
    }

    public function clearGraduatedSchool()
    {
        $address = BasicInfoGraduatedSchool::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'))->first();
            BasicInfoGraduatedSchool::where('user_id', $key)->where('id', '!=',$maxItem->id)->update(['deleted' => 1]);
        });
    }

    public function clearLearnHistory()
    {
        $address = BasicInfoLearnHistory::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'));
            BasicInfoLearnHistory::where('user_id', $key)->whereNotIn('id', $maxItem->pluck('id')->toArray())->update(['deleted' => 1]);
        });
    }

    public function clearPosition()
    {
        $address = BasicInfoPosition::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'));
            BasicInfoPosition::where('user_id', $key)->whereNotIn('id', $maxItem->pluck('id')->toArray())->update(['deleted' => 1]);
        });
    }

    public function clearWorkHistory()
    {
        $address = BasicInfoWorkHistory::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'));
            BasicInfoWorkHistory::where('user_id', $key)->whereNotIn('id', $maxItem->pluck('id')->toArray())->update(['deleted' => 1]);
        });
        
    }

    public function clearTitleInfo()
    {
        $address = BasicTitleInfo::where('deleted', 0)->get();

        $address->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'))->first();
            BasicTitleInfo::where('user_id', $key)->where('id', '!=',$maxItem->id)->update(['deleted' => 1]);
        });
        
    }
}
